const app = getApp();
let videoCtx = null

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabList: [{
        label: '好看视频',
        url: 'getHaoKanVideo'
      },
      {
        label: '六间房视频',
        url: 'getMiniVideo'
      },
      {
        label: '快手视频',
        url: 'getShortVideo'
      }
    ],
    tabActive: 0,
    pageNo: 1,
    pageSize: 20,
    items: [],
    playList: [],
    loadingVisible: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },
  getData(cb) {
    let url = this.data.tabList[this.data.tabActive].url
    this.setData({
      loadingVisible: true
    })
    wx.request({
      url: app.$api.videoInterface(url),
      data: {
        page: this.data.pageNo,
        size: this.data.pageSize
      },
      success: res => {
        if (res.statusCode == 200 && res.data.result) {
          let result = res.data.result
          let data = result.list
          if (this.data.tabActive == 1) {
            data.forEach(item => {
              item.duration = this.getTimmFormat(item.sec)
            })
          } else if (this.data.tabActive == 2) {
            data.forEach(item => {
              item.duration = this.getTimmFormat(item.duration)
            })
          }
          data.forEach(item => {
            item.picuser = app.$util.httpToHttps(item.picuser)
            item.userPic = app.$util.httpToHttps(item.userPic)
            item.playUrl = app.$util.httpToHttps(item.playUrl)
            item.playurl = app.$util.httpToHttps(item.playurl)
            item.picurl = app.$util.httpToHttps(item.picurl)
            item.coverUrl = app.$util.httpToHttps(item.coverUrl)
          })
          this.setData({
            items: this.data.items.concat(data)
          })
        }
      },
      fail: res => {
        // 
      },
      complete: res => {
        cb && cb(res)
        wx.stopPullDownRefresh();
        this.setData({
          loadingVisible: false
        })
      }
    })
  },
  getTimmFormat(sec) {
    let minutes = Math.floor(sec / 60)
    minutes = minutes > 9 ? minutes : '0' + minutes
    let seconds = sec - 60 * minutes
    seconds = seconds > 9 ? seconds : '0' + seconds
    return minutes + ':' + seconds
  },
  handleChange({
    detail
  }) {
    this.setData({
      tabActive: detail.key,
      items: []
    });
    wx.pageScrollTo({
      scrollTop: 0
    })
    setTimeout(_ => {
      this.getData()
    }, 200)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      items: [],
      pageNo: 1
    })
    wx.pageScrollTo({
      scrollTop: 0
    })
    setTimeout(_ => {
      this.getData()
    }, 200)
  },
  scroll(e) {
    console.log('scroll', e.detail);
    let playList = this.data.playList
    // 将已播放的视频暂停
    playList.forEach(item => {
      // 屏幕的尺寸
      if (e.detail.scrollTop > (item.index + 1) * 329.33 || e.detail.scrollTop < item.index * 329.33) {
        videoCtx = wx.createVideoContext(item.id, this)
        videoCtx.pause()
        this.setData({
          playList: []
        })
      }
    })
  },
  handleVideoPlay(e) {
    console.log('handleVideoPlay', e);
    let id = e.currentTarget.id
    let index = e.currentTarget.dataset.index
    let playList = this.data.playList
    // 将已播放的视频暂停
    playList.forEach(item => {
      videoCtx = wx.createVideoContext(item.id, this)
      videoCtx.pause()
    })
    this.setData({
      playList: [{
        id,
        index,
      }]
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    this.setData({
      loadingVisible: true,
      pageNo: this.data.pageNo + 1
    })
    this.getData()
  },
  scrolltolower() {
    this.setData({
      loadingVisible: true,
      pageNo: this.data.pageNo + 1
    })
    this.getData()
  }

})